//
//  IamgeFactory.m
//  New_Runway
//
//  Created by mack on 2018/1/31.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "IamgeFactory.h"

@implementation IamgeFactory

+(IamgeFactory *)sharedMemory
{
    
    __strong static IamgeFactory *instance;

    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        if ([[NSFileManager defaultManager] fileExistsAtPath:[IamgeFactory path]]) {
            
            instance = [NSKeyedUnarchiver unarchiveObjectWithData:
                        
                        [NSData dataWithContentsOfFile:[IamgeFactory path]]];
            
        }
        
        else
            
        {
            
            instance = [[IamgeFactory alloc] init];
            
        }
        
    });
    
    return instance;
    
}


+ (NSString *)path

{
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *documentDir = [paths objectAtIndex:0];
    
    NSString *dstPath = [documentDir stringByAppendingPathComponent:@"IMAGE"];
    
    return dstPath;
    
}


//归档
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    
    [aCoder encodeBool:self.control_booth forKey:@"control_booth"];
    [aCoder encodeObject:self.driving_seat forKey:@"driving_seat"];
    [aCoder encodeObject:self.engine_bay forKey:@"engine_bay"];
    [aCoder encodeObject:self.insurance forKey:@"insurance"];
    [aCoder encodeObject:self.left_anterior forKey:@"left_anterior"];
    [aCoder encodeObject:self.odograph forKey:@"odograph"];
    [aCoder encodeObject:self.open_trunk forKey:@"open_trunk"];
    [aCoder encodeObject:self.registration_license forKey:@"registration_license"];
    [aCoder encodeObject:self.right_rear forKey:@"right_rear"];
    [aCoder encodeObject:self.gear_lever forKey:@"gear_lever"];
    [aCoder encodeObject:self.tyre forKey:@"tyre"];
    [aCoder encodeObject:self.vehicle_license forKey:@"vehicle_license"];
    [aCoder encodeObject:self.vin forKey:@"vin"];
   
    
    
}

//解档
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
    
    self.control_booth = [aDecoder decodeObjectForKey:@"control_booth"];
    self.driving_seat = [aDecoder decodeObjectForKey:@"driving_seat"];
    self.engine_bay = [aDecoder decodeObjectForKey:@"engine_bay"];
    self.insurance = [aDecoder decodeObjectForKey:@"insurance"];
    self.left_anterior = [aDecoder decodeObjectForKey:@"left_anterior"];
    self.odograph = [aDecoder decodeObjectForKey:@"odograph"];
    self.open_trunk = [aDecoder decodeObjectForKey:@"open_trunk"];
    self.registration_license = [aDecoder decodeObjectForKey:@"registration_license"];
    self.right_rear = [aDecoder decodeObjectForKey:@"right_rear"];
    self.gear_lever = [aDecoder decodeObjectForKey:@"gear_lever"];
    self.tyre = [aDecoder decodeObjectForKey:@"tyre"];
    self.vehicle_license = [aDecoder decodeObjectForKey:@"vehicle_license"];
    self.vin = [aDecoder decodeObjectForKey:@"vin"];
    
    }
    
    
    return self;
}
- (void)save
{
    
    [NSKeyedArchiver archiveRootObject:self toFile:[IamgeFactory path]];
    
}
+(void)reset

{

    __strong static IamgeFactory *instance;

    [[NSFileManager defaultManager] removeItemAtPath:[IamgeFactory path] error:nil];
    
    instance = [IamgeFactory sharedMemory];
    
    instance.control_booth = nil;
    instance.driving_seat = nil;
    instance.engine_bay = nil;
    instance.insurance = nil;
    instance.left_anterior = nil;
    instance.odograph = nil;
    instance.open_trunk = nil;
    instance.registration_license = nil;
    instance.right_rear = nil;
    instance.gear_lever = nil;
    instance.tyre = nil;
    instance.vehicle_license = nil;
    instance.driving_seat = nil;
    instance.vin = nil;
    [instance save];
    
}
//-(void)delateImageWithType:(imageType)imageType{
// 
//    IamgeFactory *instance = [IamgeFactory sharedMemory];
//
//    switch (imageType) {
//        case vin_num:
//            instance.vin = @"";
//            break;
//        case right_rear:
//            instance.right_rear = @"";
//            break;
//        case driving_seat:
//            instance.driving_seat = @"";
//            break;
//        case control_booth:
//            instance.control_booth = @"";
//            break;
//        case odograph:
//            instance.odograph = @"";
//            break;
//        case open_trunk:
//            instance.open_trunk = @"";
//            break;
//        case engine_bay:
//            instance.engine_bay = @"";
//            break;
//        case gear_lever:
//            instance.gear_lever = @"";
//            break;
//        case tyre:
//            instance.tyre = @"";
//            break;
//        case insurance:
//            instance.insurance = @"";
//            break;case registration_license:
//            instance.registration_license = @"";
//            break;
//        case vehicle_license:
//            instance.vehicle_license = @"";
//            break;
//            
//        default:
//            break;
//    }
//    [instance save];
//
//}


@end
